/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.jianzhi;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.jianzhi
 * @author:LiBo/Alex
 * @create-date:2019-10-27 10:32
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:题目描述
 *
 * 在一个二维数组中，每一行都按照从左到右递增的顺序排序，每一列都按照从上到下递增的顺序排序。
 * 请完成一个函数，输入这样的一个二维数组和一个整数，判断数组中是否含有该整数。
 * 简单思路：先判断target是否在这一列，若在再进行查询，查询比下边方式更快的可用二分查找，感兴趣的可以自己尝试下二分查找。
 */
public class TestSearchArrays {



    static int [][] templArrays = {{1,2,3,4,5},{6,12,76,60},{14,23,32,40},{16,34,46,52}};



    static String solution(int[][] datas,int target){
        int i = 0;
        for(int[] column;i < templArrays.length;i++){
            column = templArrays[i];
            String param = column[0] == target ? "0,"+i:column[column.length-1] ==
                    target? column.length-1+","+i:"null";
            if("null".equals(param) && column[0]<target && column[column.length-1] > target){
                for(int num = 0;num < column.length;num++){
                    if( column[num] == target ){
                        return i+","+num;
                    }
                }
            }
        }
        return "null,null";
    }


    public boolean find(int target, int [][] array) {
        if(array.length == 0 || array[0].length == 0){
            return false;
        }
        for(int i = 0;i < array.length;i ++){
            if(target >= array[i][0] && target <= array[i][array[i].length - 1]){
                for(int j = 0; j < array[i].length; j ++){
                    if(target == array[i][j]){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args){
        System.out.println(solution(templArrays,12));
    }

}
